﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using SelfMadeMillionaire.Core;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Core.Specifications;

namespace SelfMadeMillionaire.BLL.Services
{
	public class NewsletterReadService : INewsletterReadService
	{
		private readonly INewsletterReadRepository _repo;
        private readonly ISubscriberRepository _repoSubscriber;
        private readonly INewsletterRepository _repoNewsletter;

        public NewsletterReadService(INewsletterReadRepository repo, ISubscriberRepository repoSubscriber, INewsletterRepository repoNewsletter)
		{
            _repo = repo;
            _repoSubscriber = repoSubscriber;
            _repoNewsletter = repoNewsletter;
		}

		public void MarkRead(Newsletter publication, Subscriber subs)
		{
            MarkRead(publication.ID, subs.ID);
		}

        public void MarkRead(int publicationId, int subsId)
        {
            if (!_repo.GetAll(x => x.Subscriber.ID == subsId && x.Newsletter.ID == publicationId).Any())
            {

                NewsletterRead dbItem = new NewsletterRead();
                dbItem.Newsletter = _repoNewsletter.Get(publicationId);
                dbItem.Subscriber = _repoSubscriber.Get(subsId);
                dbItem.ReadDate = DateTime.Now;

                _repo.Add(dbItem);
            }
        }

        public bool IsRead(Newsletter publication, Subscriber subs)
        {
            return _repo.GetAll(x => x.Subscriber.ID == subs.ID && x.Newsletter.ID == publication.ID).Any();
        }
	}
}